//
//  ShangPinViewController.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "ShangPinViewController.h"
#import "weddingThingModel.h"

#import "NewMerchantModel.h"

#import "MerchantDetailsViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ShangPinViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;

@property(nonatomic, retain)NewMerchantModel *new;



@end

@implementation ShangPinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"收藏的商品";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [_tableView release];
    
    [self fanguidang];
}

#pragma mark 反归档
- (void)fanguidang{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    
    // 拼接
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"ShangPin.plist"];
    self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    
        for (NewMerchantModel *new in self.arr) {
            
            NSLog(@"%@", new);
        
    }
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    NewMerchantModel *new = self.arr[indexPath.row];
    cell.textLabel.text = new.name;
    
    return cell;
}

#pragma mark 删除商品的内容
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 反归档
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"ShangPin.plist"];
    self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    
    // 删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.arr removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

/*
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MerchantDetailsViewController *production = [[MerchantDetailsViewController alloc] init];
    self.new = self.arr[indexPath.row];
    production.merchantIdStr = (NSNumber *)self.new.bid ;
    production.nameStr = self.new.name;
    
    
    [self.navigationController pushViewController:production animated:YES];
    
}

*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
